<?php

	function a($text)
	{
		return htmlspecialchars($text);
	}
	
	function t($text)
	{
		return htmlspecialchars($text);
	}	
	
	/**
	 * Parse URL
	 *
	 * @param string URL
	 * @return map{host, port, user, pass, array path, protocol, file, fragment, extension}
	 */
	function urlinfo($URL)
	{
		$parsed = array('url' => $URL);
	
		foreach (parse_url($URL) as $partName => $str)
		{
			if ($partName == 'scheme') $partName = 'protocol';
			if ($partName == 'query') $partName = 'params';
			$parsed[$partName] = $str;
			
		}
		$pathURL = $parsed['path'];
		if (strpos($pathURL, '/') === 0) 
		{
			$fromRoot = true;
		}
		if (strrpos($pathURL, '/') === (strlen($pathURL) - 1)) 
		{
			$isFolder = true;
		}
		foreach (pathinfo($pathURL) as $partName => $str)
		{
			
			
			if ($partName == 'dirname') 
			{
				$str = str_replace('\\', '/', $str);
				$dirnameTmp = array();
				foreach (explode('/', $str) as $folderName) 
				{
					if (empty($folderName)) continue;
					
					$dirnameTmp[] = urldecode($folderName);
				}
				
				if (@$fromRoot) array_unshift($dirnameTmp, '');
				
				$parsed[$partName] = $dirnameTmp;
			} 
			else 
			{
				$parsed[$partName] = urldecode($str);
			}
		}
		
		if (isset($isFolder))
		{
			$parsed['dirname'][] = $parsed['basename'];
			$parsed['basename'] = '';
		}
		
		$parsed['file'] = $parsed['basename'];
		unset($parsed['basename']);
		
		$parsed['normaly'] = url2text($parsed);
		
		return $parsed;
	}
	
	function url2text($url)
	{
		extract($url);
		$r_e_s = '';
		if (isset($protocol)) $r_e_s .= urlencode($protocol).'://';
		if (isset($host)) $r_e_s .= (isset($user) ? (isset($password) ? urlencode($user).':'.urlencode($password) : urlencode($user)).'@' : '').urlencode($host).(isset($port) ? ':'.$port : '');
		if (isset($dirname)) $r_e_s .= implode('/', array_map('urlencode', $dirname)).'/';
		if (isset($file)) $r_e_s .= urlencode($file);
		if (isset($params)) $r_e_s .= '?'.http_build_query($params);
		if (isset($fragment)) $r_e_s .= '#'.urlencode($fragment);
		
		return $r_e_s;
	}
	
	function path2text($url)
	{
		extract($url);
		$r_e_s = '';
		if (isset($dirname)) $r_e_s .= implode('/', array_map('urlencode', $dirname)).'/';
		if (isset($file)) $r_e_s .= urlencode($file);
		
		return $r_e_s;
	}
	
?>